package com.woniuxy.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.comments.entity.Goods;
import com.woniuxy.comments.util.CustomPage;
import com.woniuxy.comments.util.ResponseResult;
import com.woniuxy.comments.util.ResponseState;
import com.woniuxy.goods.mapper.GoodsMapper;
import com.woniuxy.goods.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Liu Tao
 * @Date 2023 05 15 15 53
 **/
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public ResponseResult<CustomPage<List<Goods>>> findAll(int page, int size) {
        //设置分页信息
        PageHelper.startPage(page, size);
        //执行sql
        List<Goods> goodsList = goodsMapper.findAll();
        //得到分页信息
        PageInfo<Goods> pageInfo=new PageInfo<>(goodsList);

        CustomPage<List<Goods>> customPage = new CustomPage<>();
        customPage.setCurrentPage(page);
        customPage.setSize(size);
        customPage.setTotal(pageInfo.getTotal());
        customPage.setTotalPage(pageInfo.getPages());
        customPage.setData(pageInfo.getList());


        return new ResponseResult<CustomPage<List<Goods>>>()
                .setCode(200)
                .setState(ResponseState.SELECT_SUCCESS)
                .setMessage("查询成功！")
                .setData(customPage);
    }

    @Override
    public ResponseResult<Goods> findById(int id) {
        Goods goods = goodsMapper.findById(id);

        return new ResponseResult<Goods>()
                .setCode(200)
                .setState(ResponseState.SELECT_SUCCESS)
                .setMessage("查询成功")
                .setData(goods);
    }

    @Override
    public ResponseResult<Boolean> updateByState(int id) {

        Goods goods = goodsMapper.findById(id);
        if (goods.getState()==0){
            boolean b = goodsMapper.updateByState(id,1);
            return new ResponseResult<Boolean>()
                    .setCode(b ? 200 : 500)
                    .setMessage(b ? "下架成功" : "下架失败")
                    .setState(b ? ResponseState.UPDATE_SUCCESS : ResponseState.UPDATE_FAIL)
                    .setData(b);
        }

        boolean b = goodsMapper.updateByState(id,0);
        return new ResponseResult<Boolean>()
                .setCode(b ? 200 : 500)
                .setMessage(b ? "上架成功" : "上架失败")
                .setState(b ? ResponseState.UPDATE_SUCCESS : ResponseState.UPDATE_FAIL)
                .setData(b);

    }

    @Override
    public ResponseResult<Boolean> add(Goods goods) {
        boolean b = goodsMapper.add(goods);
        return new ResponseResult<Boolean>()
                .setCode(b ? 200 : 500)
                .setMessage(b ? "添加成功" : "添加失败")
                .setState(b ? ResponseState.ADD_SUCCESS : ResponseState.ADD_FAIL)
                .setData(b);
    }

    @Override
    public ResponseResult<Boolean> update(Goods goods) {
        boolean b = goodsMapper.update(goods);
        return new ResponseResult<Boolean>()
                .setCode(b ? 200 : 500)
                .setMessage(b ? "修改成功" : "修改失败")
                .setState(b ? ResponseState.UPDATE_SUCCESS : ResponseState.UPDATE_FAIL)
                .setData(b);
    }


}
